package com.zjbti.green_dou_tx;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private EditText mEtName,mEtpphone;
    private TextView mTvshow;
    private Button mBtnadd,mBtnQuery,mBtnUpdate,mBtnDelete;

    //查询数据表中的数据：创建query（）方法
    private SQLiteDatabase db;
    private MyHelper myHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });


        //获取数据
        myHelper=new MyHelper(this);
        db=myHelper.getWritableDatabase();
        //获取控件
        init();
    }

    private void init(){
        mEtName=findViewById(R.id.ed_phonenumber);
        mEtpphone=findViewById(R.id.ed_name);
        mTvshow=findViewById(R.id.ed_text);
        mBtnadd=findViewById(R.id.btn_add);
        mBtnQuery=findViewById(R.id.btn_select);
        mBtnUpdate=findViewById(R.id.btn_change);
        mBtnDelete=findViewById(R.id.btn_delete);
        mBtnDelete.setOnClickListener(this);
        mBtnadd.setOnClickListener(this);
        mBtnQuery.setOnClickListener(this);
        mBtnUpdate.setOnClickListener(this);
    }

    private void query(){
        Cursor cursor=db.query("information",null,null,null,null,null,null);
        mTvshow.setText("");//清空界面上显示的姓名与手机号信息
        if (cursor.getCount()==0){
            Toast.makeText(this, "无此数据", Toast.LENGTH_SHORT).show();
        }else {
            while (cursor.moveToNext()){
                mTvshow.append("\n"+"Name:"+cursor.getString(2)+"; Tel:"+cursor.getString(1));
            }
        }
        cursor.close();
    }


    @Override
    public void onClick(View v) {
        String name,phone;
        ContentValues values;
        if (v.getId()==R.id.btn_add){
                name=mEtName.getText().toString();
                phone=mEtpphone.getText().toString();
                values=new ContentValues();
                values.put("name",name);
                values.put("phone",phone);
                long id=db.insert("information",null,values);
                if (id!=-1){
                    Toast.makeText(this,"信息添加成功",Toast.LENGTH_SHORT).show();
                    query();
                }else {
                    Toast.makeText(this,"信息添加失败",Toast.LENGTH_SHORT).show();
                }

        }else if (v.getId()==R.id.btn_select){
            query();
        } else if (v.getId()==R.id.btn_change) {
            Cursor cursor1=db.query("information",null,null,null,null,null,null);
            if (cursor1.getCount()==0){
                    Toast.makeText(this,"没有数据可修改",Toast.LENGTH_SHORT).show();
            }else {
                values=new ContentValues();
                values.put("phone",phone=mEtpphone.getText().toString());
                int rows;
                rows = db.update("information",values,"name=?",new String[]{mEtName.getText().toString()});
                if (rows>0){
                    Toast.makeText(this, "信息修改成功", Toast.LENGTH_SHORT).show();
                    query();
                }else {
                    Toast.makeText(this,"信息修改失败",Toast.LENGTH_SHORT).show();
                }
            }
        }else if (v.getId()==R.id.btn_delete){
            Cursor cursor2=db.query("information",null,null,null,null,null,null);
            if (cursor2.getCount()==0){
                Toast.makeText(this,"没有数据可以删",Toast.LENGTH_SHORT).show();
            }else {
                int delRows=db.delete("information",null,null);
                if (delRows>0){
                    Toast.makeText(this,"数据删除成功",Toast.LENGTH_SHORT).show();
                    mTvshow.setText("");
                }else {
                    Toast.makeText(this,"信息删除失败",Toast.LENGTH_SHORT).show();
                }
            }
        }
    }
}
class MyHelper extends SQLiteOpenHelper{
    public MyHelper(Context context){
        super(context,"itcast.db",null,1);
    }
    @Override
    public void onCreate(SQLiteDatabase db){
        db.execSQL("create table information(_id integer primary key autoincrement,name varchar(20), phone varchar(20))");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}
